﻿using Assistant;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YoucaiReader.Entities;

/**
 * 创建时间：2014/9/20 12:45:15
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace YoucaiReader.ViewModels
{
    public class LocalBookListViewModel
    {
        private ObservableCollection<BookListModel> _T_LocalBookList;        
        private BookListModel[] temporary_LocalBookList;

        public void AddBookList(BookListModel lblm_item)
        {
            this.T_LocalBookListViewModel().Count();
            this._T_LocalBookList.Add(lblm_item);
            this.SaveLocalBookList();
        }        

        public void DeleteBookList(BookListModel lblm_item)
        {
            int index = 0;
            foreach (BookListModel t_m in this.T_LocalBookListViewModel())
            {
                if (t_m.str_Id_M == lblm_item.str_Id_M)
                {
                    break;
                }
                index++;
            }
            this.T_LocalBookListViewModel().RemoveAt(index);           
            this.SaveLocalBookList();
        }


        private void SaveLocalBookList()
        {
            SettingHelper.SaveInToSettings("QQReader_LocalBookList", this.T_LocalBookListViewModel().ToList<BookListModel>());
            if (this.T_LocalBookListViewModel().Count == 0)
            {
                SettingHelper.DeleteSettings("QQReader_LocalBookList");
            }           
        }

        public ObservableCollection<BookListModel> T_LocalBookListViewModel()
        {
            if (this._T_LocalBookList == null)
            {
                if (SettingHelper.IsSettings("QQReader_LocalBookList"))
                {
                    this._T_LocalBookList = new ObservableCollection<BookListModel>(SettingHelper.GetValueFromSettings("QQReader_LocalBookList") as List<BookListModel>);
                }
                else
                {
                    this._T_LocalBookList = new ObservableCollection<BookListModel>();
                }
            }
            return this._T_LocalBookList;
        }
    }
}
